<?php


namespace addons\Minerva\api\modules\v1\controllers;

use api\controllers\OnAuthController;
use common\helpers\ResultHelper;
use common\traits\Curd;
use Yii;
use addons\Minerva\common\models\MainConfig;

/**
 * ConfigController.php
 *
 * @Class ConfigController
 * @Aackage addons\Minerva\api\modules\v1\controllers
 * @Author 小武哥<1240035958@qq.com>
 * @Date 2021/10/13 17:23
 * @Version 1.0
 */
class ConfigController extends OnAuthController
{
    use Curd;

    public $modelClass = MainConfig::class;

    public function actionIndex(){
        $config = MainConfig::find()
            ->one();
        return $config;
    }

    /**
     * @return array|MainConfig|mixed|\yii\db\ActiveRecord
     */
    public function actionCreate(){
        $post = Yii::$app->request->post();
        $model = new $this->modelClass();
        $model->attributes = $post;
        if (!$model->save()) {
            return ResultHelper::json(422, $this->getError($model));
        }
        return $model;
    }

    /**
     * @param $id
     * @return array|mixed|\yii\db\ActiveRecord
     * @throws \yii\web\NotFoundHttpException
     */
    public function actionEdit($id){
        $post = Yii::$app->request->post();
        $model = $this->findModel($id);
        $model->attributes = $post;
        if (!$model->save()) {
            return ResultHelper::json(422, $this->getError($model));
        }
        return $model;
    }
}